// JavaScript Document
function showDataTables(){
	var dname = $("#datans").val();
	$("#datatab").empty(); 
	$.ajax({
		 type: "get",
		 url:"/datas/dataTables?dname="+dname, 
		 data:{},
		 dataType:'json',
								
		 success: function(resp){
			 console.log(resp);
			 var ds = resp.data;
			 var dss = ds.split(",");
			 var htext = "";
			 $("#datatab").append("<option value=''>请选择数据表</option>");
			 for(i=0; i<dss.length; i++ ) {
				 $("#datatab").append("<option value='"+dss[i]+"'>"+dss[i]+"</option>"); 
			 } 
		 },
		 error: function(err){}    
	});	
}

function showDataFields(){
	var dname = $("#datans").val();
	var tname = $("#datatab").val();
    $("#dsfield").html("");
	//$("#limitFies").html("");
	$.ajax({
		 type: "get",
		 url:"/datas/datas?dname="+dname+"&tname="+tname, 
		 data:{},
		 dataType:'json',
								
		 success: function(resp){
			 console.log(resp);
			 var ds = resp.data;
			 var dss = ds.split(",");
			 var htext = "";
			 var htext2 = "";
			 for(i=0; i<dss.length; i++ ) {
				 htext += "<div class='checkbox'><input type='checkbox' onclick=\"checkAct('"+dss[i]+"')\" id='C"+dss[i]+"' name='ds' value='"+dss[i]+"' /><div title='"+dss[i]+"'>"+dss[i]+"</div></div>";
				 //htext2 += "<div class='checkbox'><input type='checkbox' name='df[]' value='"+dss[i]+"' /><div title='"+dss[i]+"'>"+dss[i]+"</div></div>";
			 } 
			 $("#dsfield").html(htext);
			 //$("#limitFies").html(htext2);
		 },
		 error: function(err){}    
	});	
}

var hsscore = false;

function checkAct(vue){
	var isChecked = $('#C'+vue).is(":checked");
	var sels = '<select name="S_'+vue+'" id="S'+vue+'"><option value="EMThis">当前值原值</option><option value="This">当前值数值化</option><option value="Max">多值取最大</option><option value="Min">多值取最小</option><option value="Mean">多值求平均</option><option value="Sum">求和</option></select>';
	if(isChecked){
	   $("#limitFies tbody").append("<tr id='A"+vue+"'><td width=121 valign=top class=right><div class='checkbox checkbox2'><input type='checkbox' checked=checked name='Act_"+vue+"' value='"+vue+"' /><div title='"+vue+"'>"+vue+"</div></div></td><td><div class='checkbox checkbox2'>"+sels+"</div></td><td><div class='checkbox checkbox2'><input type='text' name='GAct_"+vue+"' /></div></td></tr>");
	   scoreAct(0,vue,isChecked);
	}else{
	   $("#A"+vue).remove();
	   scoreAct(0,vue,isChecked);	
	}
}

function scoreAct(type,vue,ces){
	var outMode = $("#outputMode").val();
	if(ces){
	   if(outMode==1 || outMode==3){
		   if(!hsscore){
		      $("#scoreFies tbody").append("<tr id='outModeVue'><td width=121 valign=top class=right><span>终值</span></td><td align=center>"+scoreTo(vue)+"</td></tr>");
		      hsscore = true;
		   }
	   }else{
	       $("#scoreFies tbody").append("<tr id='Se"+vue+"'><td width=121 valign=top class=right><span>"+vue+":</span><input type='hidden' name='score_"+vue+"' value='"+vue+"' /></td><td align=center>"+scoreTo(vue)+"</td></tr>");
	   }
	}else{
		if(outMode==1 || outMode==3){
		   $("#outModeVue").remove();
		   if(hsscore){hsscore=false;}
		}else{
	       $("#Se"+vue).remove();
		}
	}
}

function scoreTo(vue){
	var so = '<select name="Cand_O_'+vue+'"><option value="0">数字型</option><option value="1">字符型</option></select></td><td align=center>';
	var sa = '<select name="Cand_A_'+vue+'"><option value="EQ">等于</option><option value="GRE">大于</option><option value="LES">小于</option><option value="SEC">区间内</option><option value="NSEC">非区间内</option><option value="NEQ">不等于</option></select></td><td align=center>';
    var sb = '<input name="Cand_B_'+vue+'" type="text" size="10" maxlength="20" /></td><td align=center>';
    var sc = '<select name="Cand_C_'+vue+'"><option value="0">=</option><option value="1">+</option><option value="2">-</option><option value="3">&times;</option><option value="4">&divide;</option><option value="5">&ne;</option></select></td><td align=center>';
    var sd = '<input name="Cand_D_'+vue+'" type="text" size="10" maxlength="20" />';
    return so+sa+sb+sc+sd;
}

$("#outputMode").change(function(){
	$("#limitFies tbody").html("");
	$("#scoreFies tbody").html("");
	$("input[type='checkbox']").removeAttr("checked"); 	
	hsscore=false;
});